﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceReference1;
using UIWindow.GlobalParameters;
using UIWindow.Helper;
using UIWindow.Models;

namespace UIWindow.Domains
{
    public static class MyMesHelper
    {
        private static ParameterGlobal parameter;
        static MyMesHelper()
        {
            parameter = Bootstrapper.GetContainer().ResolveSingleton<ParameterGlobal>();
        }

        /// <summary>
        /// 设备登录
        /// </summary>
        /// <param name="M_USERNO">用户名</param>
        /// <param name="M_PASSWORD">密码</param>
        /// <param name="M_MACHINENO">设备编号</param>
        /// <returns></returns>
        public static async Task<bool> DeviceLogin(string M_USERNO, string M_PASSWORD, string M_MACHINENO)
        {
            try
            {
                MESInterfaceSoapClient _soapClient = new MESInterfaceSoapClient(MESInterfaceSoapClient.EndpointConfiguration.MESInterfaceSoap, parameter.MESParameter.CheckUserDo);
                _soapClient.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.CheckUserDoTime);
                _soapClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.CheckUserDoTime);
                _soapClient.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.CheckUserDoTime);
                _soapClient.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.CheckUserDoTime);
                var model = await _soapClient.CheckUserDoAsync(M_USERNO, M_PASSWORD, M_MACHINENO);
                $"设备登录 返回数据：{model}".RunLog(Enums.MessageLevelType.Info, "mes");
                if (model.Contains("TRUE")) return true;
                return false;
            }
            catch (Exception ex)
            {
                $"设备登录接口异常：{ex}".RunLog(Enums.MessageLevelType.Error);
                return false;
            }
        }

        /// <summary>
        /// 设备状态上传
        /// </summary>
        /// <param name="deviecCode">设备编号</param>
        /// <param name="deviecStatus">（1、Down-设备停机，2、Wait – 设备待机，3、Warn – 设备报警，4、Run-正常运行）--备注：单词必须写对，不区分大小写</param>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="deviecErrorInfo">设备异常的详细信息数据格式为|错误代码1：详细错误原因1|错误代码2：详细错误原因2|  （按照此格式可以一次性上传多个设备异常原因）</param>
        /// <param name="userCode">用户编号</param>
        /// <returns></returns>
        public static async Task<bool> DeviecStatus(string deviecCode, int deviecStatus, string StartTime, string EndTime, string deviecErrorInfo, string userCode)
        {
            try
            {
                string str = null;
                switch (deviecStatus)
                {
                    case 1:
                        str = "Down";
                        break;
                    case 2:
                        str = "Wait";
                        break;
                    case 3:
                        str = "Warn";
                        break;
                    case 4:
                        str = "Run";
                        break;
                }
                MESInterfaceSoapClient _soapClient = new MESInterfaceSoapClient(MESInterfaceSoapClient.EndpointConfiguration.MESInterfaceSoap, parameter.MESParameter.CheckUserDo);
                _soapClient.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.DeviecStatusInfoTime);
                _soapClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.DeviecStatusInfoTime);
                _soapClient.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.DeviecStatusInfoTime);
                _soapClient.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.DeviecStatusInfoTime);
                var model = await _soapClient.DeviecStatusInfoAsync(deviecCode, str, StartTime, EndTime, deviecErrorInfo, userCode);
                $"设备状态 返回数据：{model}".RunLog(Enums.MessageLevelType.Info, "mes");
                if (model.Contains("TRUE")) return true;
                return false;
            }
            catch (Exception ex)
            {
                $"设备状态 异常：{ex.Message}".MessageDialogsUI(Enums.MessageLevelType.Error);
                return false;
            }
        }

        /// <summary>
        /// 工序校验功能
        /// </summary>
        /// <param name="M_SN">产品条码</param>
        /// <param name="M_MACHINCENO">设备编号</param>
        /// <param name="M_EMP">用户名</param>
        /// <param name="number">重连次数</param>
        /// <returns></returns>
        public static bool GroupTest(string M_SN, string M_MACHINCENO, string M_EMP, string MoNumber, string GroupName, int number)
        {
            if (number == 0) return false;
            try
            {
                MESInterfaceSoapClient _soapClient = new MESInterfaceSoapClient(MESInterfaceSoapClient.EndpointConfiguration.MESInterfaceSoap, parameter.MESParameter.CheckUserDo);
                _soapClient.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                _soapClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                _soapClient.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                _soapClient.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                var model = _soapClient.GroupTest(M_SN, M_MACHINCENO, M_EMP, MoNumber, GroupName);
                $"工序校验功能 返回数据：{model}".RunLog(Enums.MessageLevelType.Info, "mes");
                if (model.Contains("TRUE")) return true;
                return false;
            }
            catch (Exception ex)
            {
                if (number == 1) $"工序校验功能 异常：{ex.Message}".MessageDialogsUI(Enums.MessageLevelType.Error);
                return GroupTest(M_SN, M_MACHINCENO, M_EMP, MoNumber, GroupName,--number);
            }
        }

        /// <summary>
        /// 物料校验
        /// </summary>
        /// <param name="M_SN">条码</param>
        /// <param name="M_MO">制令单</param>
        /// <param name="M_USERNO">用户名</param>
        /// <param name="M_MACHINENO">设备号</param>
        /// <param name="M_KP_ITEMSN_ALL">管控料号:物料批号;数量(冒号: 分号;隔开|5201000014582:F99999999999999;10|3002000005292:888882;1)</param>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool CellBOMItemsCheck(string M_SN, string M_MO, string M_USERNO, string M_MACHINENO, string M_KP_ITEMSN_ALL, int number)
        {
            if (number == 0) return false;
            try
            {
                MESInterfaceSoapClient _soapClient = new MESInterfaceSoapClient(MESInterfaceSoapClient.EndpointConfiguration.MESInterfaceSoap, parameter.MESParameter.CheckUserDo);
                _soapClient.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                _soapClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                _soapClient.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                _soapClient.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.GroupTestTime);
                var model = _soapClient.CellBOMItemsCheck(M_SN, M_MO, M_USERNO, M_MACHINENO, M_KP_ITEMSN_ALL);
                $"物料校验 返回数据：{model}".RunLog(Enums.MessageLevelType.Info, "mes");
                if (model.Contains("TRUE")) return true;
                return false;
            }
            catch (Exception ex)
            {
                if (number == 1) $"物料校验 异常：{ex.Message}".MessageDialogsUI(Enums.MessageLevelType.Error);
                return CellBOMItemsCheck(M_SN, M_MO, M_USERNO, M_MACHINENO, M_KP_ITEMSN_ALL, --number);
            }
        }

        /// <summary>
        /// 产品过站
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool TestDataGroupInfo(ProcessParametersModel value, int number)
        {
            if (number == 0) return false;
            try
            {
                MESInterfaceSoapClient _soapClient = new MESInterfaceSoapClient(MESInterfaceSoapClient.EndpointConfiguration.MESInterfaceSoap, parameter.MESParameter.CheckUserDo);
                _soapClient.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.TestDataGroupInfoTime);
                _soapClient.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.TestDataGroupInfoTime);
                _soapClient.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.TestDataGroupInfoTime);
                _soapClient.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 0, 0, 0, parameter.MESParameter.TestDataGroupInfoTime);
                string str = System.Text.Json.JsonSerializer.Serialize(value, new System.Text.Json.JsonSerializerOptions
                {
                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                });
                $"产品过站 上报数据：{str}".RunLog(Enums.MessageLevelType.Info, "mes");
                var model = _soapClient.TestDataGroupInfo(str);
                $"产品过站 返回数据：{model}".RunLog(Enums.MessageLevelType.Info, "mes");
                if (model.Contains("产品过站成功")) return true;
                return false;
            }
            catch (Exception ex)
            {
                if (number == 1) $"工序校验功能 异常：{ex.Message}".MessageDialogsUI(Enums.MessageLevelType.Error);
                return TestDataGroupInfo(value, --number);
            }
        }
    }
}
